package dao.Supper.impl;

import dao.BaseDao;
import dao.Supper.SuperCosOrderDao;
import pojo.All;
import pojo.Customer;
import pojo.Goods;
import pojo.Orderse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SuperCosOrderDaoImpl extends BaseDao implements SuperCosOrderDao {
    /**
     * 查询数据
     * @param currentPage
     * @param pageSize
     * @param orderseId
     * @return
     */
    public List<All> getOrderseList(int currentPage, int pageSize, Long orderseId) {
        List<All> list = new ArrayList<>();
        try {
            StringBuffer sql =new StringBuffer( " SELECT o.`id` ids,g.`goodsName`, g.`goodsSeId`,c.`cusName`,o.`orderseDate`,o.`orderseAddress`,o.`orderseMoney`,o.`orderseStatus` FROM `orderse` o LEFT JOIN `goods` g ON o.`orderseGoodsId`=g.`id` INNER JOIN `customer` c ON c.`id`=o.`orderseCusId` WHERE 1=1");
            if(orderseId>0){
                sql.append(" and g.`goodsSeId`="+orderseId);
            }
            sql.append("  limit ?,?");
            Object[] params = {(currentPage - 1) * pageSize, pageSize};
            ResultSet rs = this.executeSelect(sql.toString(),params);
            while (rs.next()){
                All all = new All();
                Orderse orderse=new Orderse();
                Customer customer=new Customer();
                Goods goods=new Goods();
                int id=rs.getInt("ids");
                String goodsName=rs.getString("goodsName");
                String cusName=rs.getString("cusName");
                Date orderseDate=rs.getTimestamp("orderseDate");
                String orderseAddress=rs.getString("orderseAddress");
                double orderseMoney=rs.getDouble("orderseMoney");
                int orderseStatus=rs.getInt("orderseStatus");

                goods.setGoodsName(goodsName);
                goods.setGoodsSeId(rs.getLong("goodsSeId"));
                all.setGoods(goods);

                customer.setCusName(cusName);
                all.setCustomer(customer);

                orderse.setId(id);
                orderse.setOrderseDate(orderseDate);
                orderse.setOrderseAddress(orderseAddress);
                orderse.setOrderseMoney(orderseMoney);
                orderse.setOrderseStatus(orderseStatus);
                all.setOrderse(orderse);

                list.add(all);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return list;
    }

    /**
     * 到总数据行
     * @param orderseId
     * @return
     */
    public int orderseCount(Long orderseId) {
        int result =0;
        try {
            StringBuffer sql = new StringBuffer("SELECT COUNT(*) FROM `orderse` o LEFT JOIN `goods` g ON o.`orderseGoodsId`=g.`id` INNER JOIN `customer` c ON c.`id`=o.`orderseCusId` WHERE 1=1 ");
            if(orderseId>0){
                sql.append(" and g.`goodsSeId`= "+orderseId);
            }
            ResultSet rs = this.executeSelect(sql.toString(),null);
            while (rs.next()) {
                result = rs.getInt(1);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
           closeAll();
        }
        return result;
    }

    /**
     * 更新订单
     * @param orderse
     * @return
     */
    public int updateOrderse(Orderse orderse) {
        int ire=0;
        try {
            String sql="update orderse set orderseAddress=?,orderseMoney=?,orderseStatus=? where id=?";
            Object[] params = {orderse.getOrderseAddress(),orderse.getOrderseMoney(),orderse.getOrderseStatus(),orderse.getId()};
            ire = this.executeUpdateRows(sql, params);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
          closeAll();
        }
        return ire;
    }

    /**
     * 更新订单状态
     * @param orderseStatus
     * @param id
     * @return
     */
    public int updateOrderseStatus(int orderseStatus, Long id) {
        int delNews=0;
        try {
            String sql = "UPDATE orderse SET `orderseStatus`=? WHERE orderseStatus=0 AND `id`=?";
            Object[] params = {orderseStatus,id};
            //·µ»Ø½á¹û
            delNews = this.executeUpdateRows(sql, params);

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
          closeAll();
        }
        return delNews;
    }

    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    public All getOrderseById(Long id) {
        All all = new All();
        try {
            String sql = (" SELECT o.`id` ids,g.`goodsName`, g.`goodsSeId`,c.`cusName`,o.`orderseDate`,o.`orderseAddress`,o.`orderseMoney`,o.`orderseStatus` FROM `orderse` o LEFT JOIN `goods` g ON o.`orderseGoodsId`=g.`id` INNER JOIN `customer` c ON c.`id`=o.`orderseCusId` WHERE o.`id` =?");
            Object[] params = {id};
            ResultSet rs = this.executeSelect(sql,params);
            while (rs.next()) {

                Orderse orderse=new Orderse();
                Customer customer=new Customer();
                Goods goods=new Goods();
                String goodsName=rs.getString("goodsName");
                String cusName=rs.getString("cusName");
                Date orderseDate=rs.getTimestamp("orderseDate");
                String orderseAddress=rs.getString("orderseAddress");
                double orderseMoney=rs.getDouble("orderseMoney");
                int orderseStatus=rs.getInt("orderseStatus");

                goods.setGoodsName(goodsName);
                goods.setGoodsSeId(rs.getLong("goodsSeId"));
                all.setGoods(goods);

                customer.setCusName(cusName);
                all.setCustomer(customer);

                orderse.setId(rs.getInt("ids"));
                orderse.setOrderseDate(orderseDate);
                orderse.setOrderseAddress(orderseAddress);
                orderse.setOrderseMoney(orderseMoney);
                orderse.setOrderseStatus(orderseStatus);
                all.setOrderse(orderse);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
          this.closeAll();
        }
        return all;
    }
}
